﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMA.GENTEv1.Dados;

namespace SMA.GENTEv1
{
    public class ContratoGestor
    {
        #region Variaveis Membros
        private int _idContratoGestor;
        private Pessoa _pessoa;
        private Contrato _contrato;
        private Status _status;
        private GENTEEntities _context;
        #endregion

        #region Parametros
        public int IdContratoGestor
        {
            get { return _idContratoGestor; }
            set { _idContratoGestor = value; }
        }
        public Pessoa Pessoa
        {
            get { return _pessoa; }
            set { _pessoa = value; }
        }
        public Contrato Contrato
        {
            get { return _contrato; }
            set { _contrato = value; }
        }
        public Status Status
        {
            get { return _status; }
            set { _status = value; }
        }
        #endregion

        #region Construtores
        public ContratoGestor()
        {
            try
            {
                _context = new GENTEEntities();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public ContratoGestor(int idContratoGestor)
        {
            try
            {
                _context = new GENTEEntities();
                Carregar(idContratoGestor);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Metodos Privados
        protected void Carregar(int idContratoGestor)
        {
            try
            {
                var consulta = from cg in _context.tab_contrato_gestor
                               where cg.id_gestor == idContratoGestor
                               select new
                               {
                                   cg.id_gestor,
                                   cg.id_pessoa,
                                   cg.tab_contrato.Id_contrato,
                                   cg.tab_status.id_status
                               };

                var listConsulta = consulta.ToList();

                foreach (var list in listConsulta)
                {
                    this._idContratoGestor = list.id_gestor;
                    this._pessoa = new Pessoa(list.id_pessoa);
                    this._contrato = new Contrato(list.Id_contrato);
                    this._status = new Status(list.id_status);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Metodos Publicos

        #region Metodos de Selecao
        public List<ContratoGestor> SelectAll()
        {
            try
            {
                List<ContratoGestor> listContratoGestor = new List<ContratoGestor>();

                var consulta = from cg in _context.tab_contrato_gestor
                               select new
                               {
                                   cg.id_gestor,
                                   cg.id_pessoa,
                                   cg.tab_contrato.Id_contrato,
                                   cg.tab_status.id_status
                               };

                var listConsulta = consulta.ToList();

                foreach (var list in listConsulta)
                {
                    ContratoGestor contratoGestor = new ContratoGestor();
                    contratoGestor.IdContratoGestor = list.id_gestor;
                    contratoGestor.Pessoa = new Pessoa(list.id_pessoa);
                    contratoGestor.Contrato = new Contrato(list.Id_contrato);
                    contratoGestor.Status = new Status(list.id_status);

                    listContratoGestor.Add(contratoGestor);
                }

                return listContratoGestor;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<ContratoGestor> SelectPor(int idContrato)
        {
            try
            {
                List<ContratoGestor> listContratoGestor = new List<ContratoGestor>();

                var consulta = from cg in _context.tab_contrato_gestor
                               where cg.tab_contrato.Id_contrato == idContrato
                               select new
                               {
                                   cg.id_gestor,
                                   cg.id_pessoa,
                                   cg.tab_contrato.Id_contrato,
                                   cg.tab_status.id_status
                               };

                var listConsulta = consulta.ToList();

                foreach (var list in listConsulta)
                {
                    ContratoGestor contratoGestor = new ContratoGestor();
                    contratoGestor.IdContratoGestor = list.id_gestor;
                    contratoGestor.Pessoa = new Pessoa(list.id_pessoa);
                    contratoGestor.Contrato = new Contrato(list.Id_contrato);
                    contratoGestor.Status = new Status(list.id_status);

                    listContratoGestor.Add(contratoGestor);
                }

                return listContratoGestor;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<ContratoGestor> SelectPorSemGestor(int idContrato, int idGestor)
        {
            try
            {
                List<ContratoGestor> listContratoGestor = new List<ContratoGestor>();

                var consulta = from cg in _context.tab_contrato_gestor
                               where cg.tab_contrato.Id_contrato == idContrato
                               && cg.id_pessoa != idGestor
                               select new
                               {
                                   cg.id_gestor,
                                   cg.id_pessoa,
                                   cg.tab_contrato.Id_contrato,
                                   cg.tab_status.id_status
                               };

                var listConsulta = consulta.ToList();

                foreach (var list in listConsulta)
                {
                    ContratoGestor contratoGestor = new ContratoGestor();
                    contratoGestor.IdContratoGestor = list.id_gestor;
                    contratoGestor.Pessoa = new Pessoa(list.id_pessoa);
                    contratoGestor.Contrato = new Contrato(list.Id_contrato);
                    contratoGestor.Status = new Status(list.id_status);

                    listContratoGestor.Add(contratoGestor);
                }

                return listContratoGestor;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<ContratoGestor> SelectPorGestor(int idGestor)
        {
            try
            {
                List<ContratoGestor> listContratoGestor = new List<ContratoGestor>();

                var consulta = from cg in _context.tab_contrato_gestor
                               where cg.id_pessoa == idGestor
                               select new
                               {
                                   cg.id_gestor,
                                   cg.id_pessoa,
                                   cg.tab_contrato.Id_contrato,
                                   cg.tab_status.id_status
                               };

                var listConsulta = consulta.ToList();

                foreach (var list in listConsulta)
                {
                    ContratoGestor contratoGestor = new ContratoGestor();
                    contratoGestor.IdContratoGestor = list.id_gestor;
                    contratoGestor.Pessoa = new Pessoa(list.id_pessoa);
                    contratoGestor.Contrato = new Contrato(list.Id_contrato);
                    contratoGestor.Status = new Status(list.id_status);

                    listContratoGestor.Add(contratoGestor);
                }

                return listContratoGestor;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Metodos de Pesistencia
        public void Salvar()
        {
            try
            {
                if (_idContratoGestor == 0)
                {
                    tab_contrato_gestor tabContratoGestor = new tab_contrato_gestor();
                    tabContratoGestor.id_pessoa = _pessoa.IdPessoa;
                    tabContratoGestor.tab_contratoReference.EntityKey = new System.Data.EntityKey("GENTEEntities.tab_contrato", "Id_contrato", _contrato.IdContrato);
                    tabContratoGestor.tab_statusReference.EntityKey = new System.Data.EntityKey("GENTEEntities.tab_status", "id_status", _status.IdStatus);

                    _context.AddTotab_contrato_gestor(tabContratoGestor);
                    _context.SaveChanges();
                }
                else
                {
                    tab_contrato_gestor tabContratoGestor = (from cg in _context.tab_contrato_gestor
                                                             where cg.id_gestor == _idContratoGestor
                                                             select cg).First();

                    tabContratoGestor.id_pessoa = _pessoa.IdPessoa;
                    tabContratoGestor.tab_contratoReference.EntityKey = new System.Data.EntityKey("GENTEEntities.tab_contrato", "Id_contrato", _contrato.IdContrato);
                    tabContratoGestor.tab_statusReference.EntityKey = new System.Data.EntityKey("GENTEEntities.tab_status", "id_status", _status.IdStatus);

                    _context.SaveChanges();
                }

                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Excluir()
        {
            try
            {
                if (_idContratoGestor != 0)
                {
                    var consulta = from cg in _context.tab_contrato_gestor
                                   where cg.id_gestor == _idContratoGestor
                                   select cg;

                    var listConsulta = consulta.ToList();

                    foreach (var list in listConsulta)
                    {
                        _context.DeleteObject(list);
                        _context.SaveChanges();
                    }
                }
                else
                {
                    var consulta = from cg in _context.tab_contrato_gestor
                                   where cg.tab_contrato.Id_contrato == _contrato.IdContrato
                                   && cg.id_pessoa == Pessoa.IdPessoa
                                   select cg;

                    var listConsulta = consulta.ToList();

                    foreach (var list in listConsulta)
                    {
                        _context.DeleteObject(list);
                        _context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #endregion
    }
}
